package br.edu.infnet.avaliacao.model;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name = "avaliacao", catalog = "avaliacao")
public class Avaliacao implements java.io.Serializable {

	private static final long serialVersionUID = -2912072599650508307L;
	private Integer id;
	private Questionario questionario;
	private Matricula matricula;
	private Date dataInicial;
	private Date dataFinal;
	private Character status;
	private Set<AvaliacaoQuestao> avaliacaoQuestaos = new HashSet<AvaliacaoQuestao>(
			0);

	public Avaliacao() {
	}

	public Avaliacao(Questionario questionario, Matricula matricula) {
		this.questionario = questionario;
		this.matricula = matricula;
	}

	public Avaliacao(Questionario questionario, Matricula matricula,
			Date dataInicial, Date dataFinal, Character status,
			Set<AvaliacaoQuestao> avaliacaoQuestaos) {
		this.questionario = questionario;
		this.matricula = matricula;
		this.dataInicial = dataInicial;
		this.dataFinal = dataFinal;
		this.status = status;
		this.avaliacaoQuestaos = avaliacaoQuestaos;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "questionario_id", nullable = false)
	public Questionario getQuestionario() {
		return this.questionario;
	}

	public void setQuestionario(Questionario questionario) {
		this.questionario = questionario;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "matricula_id", nullable = false)
	public Matricula getMatricula() {
		return this.matricula;
	}

	public void setMatricula(Matricula matricula) {
		this.matricula = matricula;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "data_inicial", length = 19)
	public Date getDataInicial() {
		return this.dataInicial;
	}

	public void setDataInicial(Date dataInicial) {
		this.dataInicial = dataInicial;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "data_final", length = 19)
	public Date getDataFinal() {
		return this.dataFinal;
	}

	public void setDataFinal(Date dataFinal) {
		this.dataFinal = dataFinal;
	}

	@Column(name = "status", length = 1)
	public Character getStatus() {
		return this.status;
	}

	public void setStatus(Character status) {
		this.status = status;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "avaliacao")
	public Set<AvaliacaoQuestao> getAvaliacaoQuestaos() {
		return this.avaliacaoQuestaos;
	}

	public void setAvaliacaoQuestaos(Set<AvaliacaoQuestao> avaliacaoQuestaos) {
		this.avaliacaoQuestaos = avaliacaoQuestaos;
	}
	
	@Transient
	public boolean isAberta(){
		Calendar dataAtual = Calendar.getInstance();
		if(dataAtual.getTimeInMillis() < this.dataFinal.getTime() && this.status.equals('A') ){
			return true;
		}else{
			return false;
		}
	}

}
